<?php
/**
 * $Id: user_content.php 642 2010-07-27 13:21:19Z varun $
 * 
 * This file can not be redistributed to anyone without the permission of Giga Promoters
 * Copyright 2005 - 2008, Giga Promoters
 * 						  608 Aggarwal Cyber Plaza
 *						  Netaji Subhash Place, Delhi - 110034
 *						  www.gigapromoters.com
 * 
 *
 * This is parent of parent class of a controller and should not be edited ever without permission.
 * @version 			$Rev: 642 $
 * @lastModifiedBy		$LastChangedBy: varun $
 * @lastChanged			$Date: 2010-07-27 18:51:19 +0530 (Tue, 27 Jul 2010) $
 * @copyright 			Giga Promoters (www.gigapromoters.com)
 * 
 */

/**
 * This is the mysterious behavior, one of the strangest creation. 
 * If you use it, use it at your own risk :)
 * If you volunteer to fix it, your courage shall be appreciated. Just let me know - Abhimanyu
 *
 */
class UserContentBehavior extends ModelBehavior {

	var $settings;
	var $statuses = array('0'=>'Moderation', '1'=>'Published', '2'=>'Spam');

	//var $captchaCode;

	function setup(&$model, $config=array()) {

		$this->settings[$model->name] = $config;
		if(!isset($config['moderation']))
		{
			$this->settings[$model->name]['moderation'] =  Configure::read($model->name.'.moderation');
		}
		extract($this->settings[$model->name]);

		if(@!$model->_schema[$field])
		{
			pr($model->_schema);
			echo sprint(__("No status field inside %s. Either add this field, or remove UserContent behavior."),$model->name);
			exit;
		}
		
		/* Enhance the binding with user model 
		if($model->name!='User')
			$model->bindModel(array('belongsTo' => array(
				'User' => array('className' => 'Users.User',
								'foreignKey' => 'user_id',
								'conditions' => '',
								'fields' => '',
								'order' => ''
							))
				));
		*/
	}

	// add status before validation
	function beforeValidate(&$model)
	{
		if(!isset($model->data[$model->name]['id']))
		{
			extract($this->settings[$model->name]);
			if($moderation=='on')
			{
				if(!isset($model->data[$model->name][$field]) && !isset($model->data[$model->name]['id'])) {
					$model->data[$model->name][$field] = UC_STATUS_PENDING;    
				}
			}

			elseif($moderation=='off')
			{
				if(!isset($model->data[$model->name][$field]) && !isset($model->data[$model->name]['id'])) {
					$model->data[$model->name][$field] = UC_STATUS_APPROVED;    
				}
			}
		}
	}



	//uncomment if something stopped working
	/*
	function beforeFind(&$model)
	{
		
		if($model->name!='User')
			$model->bindModel(array('belongsTo' => array(
				'User' => array('className' => 'User',
								'foreignKey' => 'user_id',
								'conditions' => '',
								'fields' => '',
								'order' => ''
							))
				));
		
	}
	*/



	/**
	 * This function should be used at the end of controller function to generate captcha code and store it to session
	 * Captch helper should be included, and used in view to output the image and textfield. Rest it will handle itself.
	 * This function also needs some serious refactoring.
	 *
	 * @param unknown_type $model
	 */
	function enableCaptcha(&$model)
	{
		$model->params['enableCaptcha']='1';

		// avoid regenerating code again if data is posted
		if(!isset($_POST['data']))
		{
			$code = $this->_generateCode(6);
			$_SESSION['verification_code'] = $code;
			if(defined('GIGA_CMS_TEST'))
			{
				file_put_contents(APP.'test', $code);
			}
		}
	}


	//move to beforeValidate above
	function beforeSave(&$model)
	{
		/*
		echo "time to verify code";
		echo $model->params['enableCaptcha'];
		exit;
		*/
		
		// automatically detect session user and store it in user_id field
		if(isset($_SESSION['Auth']['User']))
			$model->data[$model->name]['user_id'] = $_SESSION['Auth']['User']['id'];

		if(!isset($model->params['enableCaptcha']) || $model->params['enableCaptcha']!='1')
			return true;
		
		if($model->data[$model->name]['verification_code'] == $_SESSION['verification_code'])
		{
			return true;
		}
		else
		{
			unset($model->data[$model->name]['verification_code']);
			// disabled because of saveAll() bug
			$model->validationErrors['verification_code'] = 'Invalid code';
			return false;
		}
		parent::beforeSave();
	}



}
?>